package com.zxbg.controller.systemtoolsController;

import com.zxbg.controller.Code;
import com.zxbg.controller.Result;
import com.zxbg.domain.InfoBlock;
import com.zxbg.domain.Process;
import com.zxbg.domain.ProcessRule;
import com.zxbg.domain.ProcessRuleContent;
import com.zxbg.service.systemtoolsService.ProcessManagePlatformService;
import com.zxbg.service.systemtoolsService.RoleManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/processManagePlatform")
public class ProcessManagePlatformController {
    @Autowired
    ProcessManagePlatformService processManagePlatformService;
    @Autowired
    RoleManagementService roleManagementService;

    @PostMapping
    public Result addRule(@RequestBody ProcessRule processRule, @RequestHeader Integer tenant_id, @RequestHeader String employee_id) {
        processRule.setTenant_id(tenant_id);
        processRule.setCreator_id(employee_id);
        processRule.setUpdater_id(employee_id);
        String res = processManagePlatformService.addRule(processRule);
        return new Result(res.equals("") ? Code.SAVE_OK : Code.SAVE_ERR, res, res);
    }

    @PostMapping("/getAllProcessRule")
    public Result getAllProcessRule(@RequestBody String key, @RequestHeader Integer tenant_id, @RequestHeader String employee_id) {
        boolean ifSysadmin =  roleManagementService.getIfSysadmin(employee_id, tenant_id);
        return new Result(Code.GET_OK, processManagePlatformService.getAllProcessRule(key, tenant_id, ifSysadmin, employee_id));
    }

    @PutMapping
    public Result updateProcessRule(@RequestBody ProcessRule processRule, @RequestHeader Integer tenant_id, @RequestHeader String employee_id) {
        processRule.setTenant_id(tenant_id);
        processRule.setUpdater_id(employee_id);
        return new Result(Code.UPDATE_OK, processManagePlatformService.updateProcessRule(processRule));
    }

    @DeleteMapping("/{process_rule_code}")
    public Result deleteProcessRule(@PathVariable String process_rule_code, @RequestHeader Integer tenant_id) {
        return new Result(Code.DELETE_OK, processManagePlatformService.deleteProcessRule(process_rule_code, tenant_id));
    }

    @GetMapping("/ruleEdit/{process_rule_code}")
    public Result getProcessRuleContent(@PathVariable String process_rule_code, @RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, processManagePlatformService.getProcessRuleContent(process_rule_code, tenant_id));
    }

    @PostMapping("/ruleEdit/{status}")
    public Result saveProcessRuleContent(@RequestBody List<ProcessRuleContent> processRuleContentList, @RequestHeader Integer tenant_id, @PathVariable Integer status) {
        return new Result(Code.SAVE_OK, processManagePlatformService.saveProcessRuleContent(processRuleContentList, tenant_id, status));
    }

    @PutMapping("/ruleEdit/{status}")
    public Result updateProcessRuleContent(@RequestBody List<ProcessRuleContent> processRuleContentList, @RequestHeader Integer tenant_id, @PathVariable Integer status, @RequestHeader String employee_id) {
        return new Result(Code.SAVE_OK, processManagePlatformService.updateProcessRuleContent(processRuleContentList, tenant_id, status, employee_id));
    }

    @PostMapping("/getProcessList")
    public Result getProcessList(@RequestBody Map<String, Object> search_info, @RequestHeader Integer tenant_id, @RequestHeader String employee_id) {
        List<Process> processList = processManagePlatformService.getProcessList(search_info, tenant_id, employee_id);
        List<Process> resList = new ArrayList<>();
        Integer startNum = ((Integer)search_info.get("current_page") - 1) * (Integer)search_info.get("page_size");
        Integer endNum = (Integer)search_info.get("current_page") * (Integer)search_info.get("page_size") - 1;
        for (int i = 0; i < processList.size(); i++) {
            if(i >= startNum && i <= endNum) {
                resList.add(processList.get(i));
            }
        }
        Map<String,Object> first_map = new HashMap<>();
        Map<String,Object> second_map = new HashMap<>();
        second_map.put("page_size", search_info.get("page_size"));
        second_map.put("current_page", search_info.get("current_page"));
        second_map.put("total", processList.size());
        first_map.put("page", second_map);
        first_map.put("rows", resList);
        return new Result(Code.GET_OK, first_map);
    }
}
